<template>
  <div class="mb-6 flex items-center justify-between">
    <div>
      <h2 class="mb-2 text-sm">{{ $t('总资产') }}</h2>
      <div class="font-extrabold text-2xl"> {{ data.symbol }}{{ numberToCurrencyNo(data.total_assets) }}</div>
    </div>
  </div>

  <el-row :gutter="20">
    <el-col :span="8">
      <el-card class="box-card">
        <div>
          <p class="mb-1 text-sm">{{ $t('可用资产') }}</p>
          <div class="font-extrabold text-xl"> {{ data.symbol }}{{ numberToCurrencyNo(data.total_available) }}</div>
        </div>
      </el-card>
    </el-col>
    <el-col :span="8">
      <router-link class="text-[#e54c5f] cursor-pointer" to="/account/assets/pending">
      <el-card class="box-card">
        <div>
          <p class="flex justify-between">
            <span class="text-sm">{{ $t('在途资产') }}</span>
            <el-button round size="small" :icon="MoreFilled">{{ $t('查看') }}</el-button>
          </p>
          <div class="font-extrabold text-xl"> {{ data.symbol }}{{ numberToCurrencyNo(data.total_pending) }}</div>
        </div>
      </el-card>
    </router-link>
    </el-col>
    <el-col :span="8">
      <router-link class="text-[#e54c5f] cursor-pointer" to="/account/assets/freeze">
      <el-card class="box-card">
        <div>
          <p class="flex justify-between">
            <span class="text-sm">{{ $t('冻结资产') }}</span>
            <el-button round size="small" :icon="MoreFilled">{{ $t('查看') }}</el-button>
          </p>
          <div class="font-extrabold text-xl"> {{ data.symbol }}{{ numberToCurrencyNo(data.total_frozen) }}</div>
        </div>
      </el-card>
      </router-link>
    </el-col>
  </el-row>
  <h3 class="my-5">{{ $t('资产列表') }}</h3>
  <el-table :data="assetsList" v-loading="loading">
      <el-table-column prop="currency" :label="$t('币种')" align="center"/>
      <el-table-column :label="$t('总金额')" align="right">
        <template #default="{ row }">
          {{ numberToCurrencyNo(row.balance) }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('可用金额')" align="right">
        <template #default="{ row }">
          {{ numberToCurrencyNo(row.available) }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('冻结金额')" align="right">
        <template #default="{ row }">
          {{ numberToCurrencyNo(row.freeze) }}
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('操作')">
        <!--<template #header>-->
        <!--    <el-icon><MoreFilled /></el-icon>-->
        <!--</template>-->
        <template #default="scope">
        <el-button round :icon="MoreFilled" class="cursor-pointer"
        @click="handleDetails(scope.row)">{{ $t('查看') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="mt-5 flex justify-center">
      <el-pagination background
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageable.total"
        :default-page-size="pageable.pageSize"
        :default-current-page="pageable.pageNum"
        :page-sizes="[10, 25, 50, 100]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
</template>

<script setup>
import { reactive, ref } from "vue";
//import { CountTo } from "vue3-count-to";
import { getBalanceApi, getAssetsApi } from "@/api/assetDetails/index";
import { MoreFilled } from '@element-plus/icons-vue'
import { numberToCurrencyNo } from '@/utils/utils'
// 格式化数字，保留小数点后两位
// const formatNumber = (num) => {
//   return num.toFixed(2);
// };
// const formatNumber = (num) => {
//   return parseFloat(num.toFixed(2));
// };
// const decimalPlaces = res.decimalPlaces; // 从响应数据中获取小数位数
import { useRouter } from "vue-router";
const loading = ref(false);
const router = useRouter();
const data = reactive({
  total_assets: 0, //总资产余额
  total_available: 0, // 可用总资产
  total_frozen: 0, // 冻结总资产
  total_pending: 0, // 待入账总资产
  symbol: "$", //币种符号
});
// 分页
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
const assetsList = ref([]);
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
  };
};
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
// 条数
const handleSizeChange = number => {
  pageable.pageSize = number;
  getTbaleData();
};
// 查看
const handleDetails = ({ ...row }) => {
  router.replace({
    path: "/account/assets/records",
    query: { currency: row.currency },
  });
};
// 资产
const getData = async () => {
  try {
    const res = await getBalanceApi();
    res.total_assets = Number(res.total_assets);
    res.total_available = Number(res.total_available);
    res.total_frozen = Number(res.total_frozen);
    res.total_pending = Number(res.total_pending);
    Object.assign(data, res);
  } catch (error) { }
};
// 资产列表
const getAssets = async () => {
  try {
    const res = await getAssetsApi(param());
    // console.log(res);
    assetsList.value = res.list;
  } catch (error) { }
};
Promise.all([getData(), getAssets()]);
</script>
